package com.winteam.View;

import com.winteam.Bean.Client;
import com.winteam.Bean.RoomInfo;
import com.winteam.Controler.Select;

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.*;
import javax.swing.border.EmptyBorder;
import javax.swing.event.TableModelEvent;
import javax.swing.event.TableModelListener;
import javax.swing.table.DefaultTableModel;
import java.awt.Color;
import java.awt.Font;
import java.awt.event.*;
import java.util.List;
import java.util.Vector;

public class BillingInformationView extends JFrame {

    private JPanel contentPane;
    private JTextField name;
    private JTextField sfz, day;
    private JTextField address;
    private JTextField rember, iphone, yj;

    JComboBox sex;
    private Select select = new Select();
    private JLabel roomnumber, price;


    Vector<String> header = new Vector<>();

    Vector<Vector<String>> R_body = new Vector<>();
    Vector rightheader = new Vector();
    Vector<Vector<String>> rightBody = new Vector();
    /*
    要移除的行数
     */
    int row = 0;
    DefaultTableModel leftModel = new DefaultTableModel();
    DefaultTableModel rightModel = new DefaultTableModel();
    private int r_row = 0;

    /**
     * Create the frame.
     */
    public BillingInformationView() {
        setDefaultCloseOperation(1);
        setTitle("散客开单");
        setSize(730, 700);
        setLocationRelativeTo(null);
        contentPane = new JPanel();
        header.add("可供房间");
        contentPane.setLayout(null);
        setContentPane(contentPane);

        roomnumber = new JLabel("");
        roomnumber.setForeground(Color.BLUE);
        roomnumber.setFont(new Font("宋体", Font.BOLD, 21));
        roomnumber.setBounds(94, 50, 130, 18);
        contentPane.add(roomnumber);

        JLabel lblNewLabel = new JLabel("开单信息");
        lblNewLabel.setFont(new Font("微软雅黑", Font.BOLD, 17));
        lblNewLabel.setLabelFor(this);
        lblNewLabel.setBounds(338, 0, 79, 37);
        contentPane.add(lblNewLabel);

        JComboBox roomtype = new JComboBox();
        roomtype.addItemListener(new ItemListener() {
            @Override
            public void itemStateChanged(ItemEvent e) {
                String type = roomtype.getSelectedItem().toString();


                rightBody.clear();
                if (rightBody != null) {
                    R_body.clear();
                    leftModel.setDataVector(R_body, rightheader);
                }

                setTableModel(type);
                rightModel.setDataVector(rightBody, header);
                roomnumber.setText("");

            }
        });
        List<String> roomlx = select.getRoomlx();
        roomtype.setModel(new DefaultComboBoxModel(roomlx.toArray()));
        roomtype.setForeground(Color.BLUE);
        roomtype.setFont(new Font("宋体", Font.BOLD, 21));
        roomtype.setBounds(350, 45, 150, 30);
        contentPane.add(roomtype);

        price = new JLabel("239.0");
        price.setForeground(Color.RED);
        price.setBackground(Color.RED);
        price.setFont(new Font("Comic Sans MS", Font.BOLD, 19));
        price.setBounds(628, 48, 72, 18);
        contentPane.add(price);

        JLabel lblNewLabel_1 = new JLabel("");
        lblNewLabel_1.setBackground(new Color(199, 183, 143));
        lblNewLabel_1.setOpaque(true);
        lblNewLabel_1.setBounds(0, 0, 712, 37);
        contentPane.add(lblNewLabel_1);

        JLabel lblNewLabel_3 = new JLabel("房间类型 :");
        lblNewLabel_3.setFont(new Font("宋体", Font.BOLD, 15));
        lblNewLabel_3.setBounds(260, 50, 88, 18);
        contentPane.add(lblNewLabel_3);

        JLabel lblNewLabel_3_1 = new JLabel("主客房间 :");
        lblNewLabel_3_1.setFont(new Font("宋体", Font.BOLD, 15));
        lblNewLabel_3_1.setBounds(10, 50, 90, 18);
        contentPane.add(lblNewLabel_3_1);

        JLabel lblNewLabel_5 = new JLabel("姓 名 :");
        lblNewLabel_5.setFont(new Font("宋体", Font.BOLD, 15));
        lblNewLabel_5.setBounds(10, 87, 66, 28);
        contentPane.add(lblNewLabel_5);

        JLabel lblNewLabel_3_2 = new JLabel("预设单价 :");
        lblNewLabel_3_2.setFont(new Font("宋体", Font.BOLD, 15));
        lblNewLabel_3_2.setBounds(540, 50, 82, 18);
        contentPane.add(lblNewLabel_3_2);

        JLabel lblNewLabel_2 = new JLabel("");

        lblNewLabel_2.setBackground(new Color(231, 215, 183));
        lblNewLabel_2.setOpaque(true);
        lblNewLabel_2.setBounds(0, 37, 712, 37);
        contentPane.add(lblNewLabel_2);

        name = new JTextField();
        name.setBounds(69, 84, 153, 31);
        contentPane.add(name);
        name.setColumns(10);

        JLabel lblNewLabel_6 = new JLabel("身份证:");
        lblNewLabel_6.setFont(new Font("宋体", Font.BOLD, 15));
        lblNewLabel_6.setBounds(236, 90, 91, 23);
        contentPane.add(lblNewLabel_6);

        sfz = new JTextField();
        sfz.setColumns(10);
        sfz.setBounds(295, 84, 261, 31);
        contentPane.add(sfz);

        JLabel label = new JLabel("性别:");
        label.setFont(new Font("宋体", Font.BOLD, 15));
        label.setBounds(570, 92, 72, 18);
        contentPane.add(label);

        sex = new JComboBox();
        sex.setModel(new DefaultComboBoxModel(new String[]{"男", "女"}));
        sex.setBounds(620, 86, 62, 26);
        contentPane.add(sex);
		/*
		手机号码
		 */
        JLabel label_1_2 = new JLabel("手机号码:");
        label_1_2.setFont(new Font("宋体", Font.BOLD, 15));
        contentPane.add(label_1_2);

        iphone = new JTextField();
        iphone.setBounds(94, 120, 340, 28);

        iphone.setColumns(10);
        contentPane.add(iphone);
        label_1_2.setBounds(10, 115, 90, 37);
        JLabel label_1 = new JLabel("地址信息:");
        label_1.setFont(new Font("宋体", Font.BOLD, 15));
        label_1.setBounds(10, 146, 90, 37);
        contentPane.add(label_1);

        address = new JTextField();
        address.setBounds(94, 150, 528, 28);
        contentPane.add(address);
        address.setColumns(10);

        JLabel label_1_1 = new JLabel("备注信息:");
        label_1_1.setFont(new Font("宋体", Font.BOLD, 15));
        label_1_1.setBounds(10, 187, 90, 37);
        contentPane.add(label_1_1);

        rember = new JTextField();
        rember.setColumns(10);
        rember.setBounds(94, 191, 528, 28);
        contentPane.add(rember);

        JLabel label_1_12 = new JLabel("实收押金:");
        label_1_12.setFont(new Font("宋体", Font.BOLD, 15));
        label_1_12.setBounds(10, 220, 90, 37);
        contentPane.add(label_1_12);


        yj = new JTextField();
        yj.setColumns(10);
        yj.setBounds(94, 227, 40, 25);
        contentPane.add(yj);
        JLabel label_1_121 = new JLabel("预住天数:");
        label_1_121.setFont(new Font("宋体", Font.BOLD, 15));
        label_1_121.setBounds(220, 220, 90, 37);
        contentPane.add(label_1_121);


        day = new JTextField();
        day.setColumns(10);
        day.setBounds(300, 227, 40, 25);
        contentPane.add(day);

        JButton sure = new JButton("确定", new ImageIcon(this.getClass().getResource("img/modi3.gif")));
        sure.setOpaque(true);
        sure.setBackground(new Color(231, 215, 183));

        /**
         * 确定按钮
         */
        sure.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                Client c = getClient();
                if (c != null) {
                    if (select.addClient(c)) {
                        String c_name = c.getC_name();
                        int clientId = select.findClientId(c_name);
                        //添加数据的方法
                        addCheck_in(clientId);
                        dispose();
                    }
                }

            }
        });
        sure.setBounds(69, 598, 164, 38);
        contentPane.add(sure);

        JButton no = new JButton("取消", new ImageIcon(this.getClass().getResource("img/cancel.gif")));
        no.setOpaque(true);
        no.setBackground(new Color(231, 215, 183));

        /*
         * 取消按钮
         */
        no.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                //  setVisible(false);
                dispose();
            }
        });
        no.setBounds(435, 598, 164, 38);
        contentPane.add(no);

        setTableModel("标准单人间");
        JTable leftTable = new JTable(leftModel);
        /**
         * System.out.println(leftModel.getValueAt(row, 0).toString());
         * 				row =leftTable.getSelectedRow();
         * 				System.out.println(row);
         */
        leftTable.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                row = leftTable.getSelectedRow();
            }
        });

        JScrollPane add = new JScrollPane(leftTable);
        add.setBounds(28, 289, 261, 296);
        contentPane.add(add);

        rightheader.add("开单房间");
        rightModel.setDataVector(rightBody, rightheader);
        JTable rightTable = new JTable(rightModel);
        JScrollPane adds = new JScrollPane(rightTable);
        rightTable.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                r_row = rightTable.getSelectedRow();
            }
        });
        adds.setBounds(407, 289, 261, 296);
        contentPane.add(adds);

        JButton right = new JButton("", new ImageIcon(this.getClass().getResource("img/right.gif")));

        right.setContentAreaFilled(false);//设置按钮透明背景
        right.setFocusPainted(false);//去掉按钮周围的焦点框
        right.setBorderPainted(false);//去边框
        right.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                if (rightBody.size() == 4) {
                    JOptionPane.showMessageDialog(null, "散单最多只能开四间房");
                    return;
                }
                try {

                    addLeftModel(leftModel.getValueAt(row, 0).toString());
                    leftModel.removeRow(row);
                } catch (ArrayIndexOutOfBoundsException e2) {
                    System.out.println("索引异常");
                    JOptionPane.showMessageDialog(null, "请选择一行!!");
                }
            }
        });
        right.setBounds(332, 384, 50, 50);
        contentPane.add(right);

        JButton left = new JButton("", new ImageIcon(this.getClass().getResource("img/left.gif")));
        left.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {

                try {
                    addRight(rightModel.getValueAt(r_row, 0).toString());
                    rightModel.removeRow(r_row);
                } catch (ArrayIndexOutOfBoundsException e2) {
                    System.out.println("索引异常");
                    JOptionPane.showMessageDialog(null, "请选择一行!!");
                }
            }
        });
        left.setContentAreaFilled(false);//设置按钮透明背景
        left.setFocusPainted(false);//去掉按钮周围的焦点框
        left.setBorderPainted(false);//去边框
        left.setBounds(332, 439, 50, 50);
        contentPane.add(left);

        JLabel lblNewLabel_8 = new JLabel("追加房间");
        lblNewLabel_8.setFont(new Font("宋体", Font.BOLD, 21));
        lblNewLabel_8.setBounds(28, 258, 133, 18);
        contentPane.add(lblNewLabel_8);

        JLabel lblNewLabel_4 = new JLabel("");
        lblNewLabel_4.setBounds(0, 74, 712, 579);
        lblNewLabel_4.setBackground(new Color(231, 215, 183));
        lblNewLabel_4.setOpaque(true);
        contentPane.add(lblNewLabel_4);

        setVisible(true);
    }

    private void addCheck_in(int clientId) {
        if (yj.getText().equals("") && day.getText().equals("")) {
            JOptionPane.showMessageDialog(null, "请填入完整信息");
        }
        int roomId = 0;
        for (Vector<String> roomInfos : rightBody) {

            System.out.println(roomInfos.get(0));
            roomId = select.findRoomId(roomInfos.get(0));
            select.addCheck_in(roomId, clientId, rightBody.size(), yj.getText(), day.getText());
            /**
             * 把所有的的房间号都变成占用状态 roomId
             */
            select.updateRoomState(roomId);
        }


        JOptionPane.showMessageDialog(null, "开单完成");

    }


    /*

    根据查去道德id
     */
    private Client getClient() {
        /**
         * 首先获取到用户的信息储存到数据库里面然后
         *

         */
        String names = name.getText();
        String sfzs = sfz.getText();
        String sexs = this.sex.getSelectedItem().toString();
        String iphones = iphone.getText();
        String address = this.address.getText();
        String rember = this.rember.getText();

        if (names.equals("") && sfzs.equals("") && iphones.equals("") && address.equals("") && rember.equals("")) {
            JOptionPane.showMessageDialog(null, "请填入完整信息");
            return null;
        }
        return new Client(1, names, sexs, "身份证", sfzs, address, iphones, rember);
    }

    private void addRight(String toString) {
        Vector v = new Vector();
        v.add(toString);

        R_body.add(v);
        leftModel.setDataVector(R_body, header);
    }

    /**
     * 设置右边的table的信息
     *
     * @param roomtype
     */
    private void setTableModel(String roomtype) {

        for (RoomInfo r : select.selecRoomTypeinfos(roomtype)) {
            Vector v = new Vector();
            this.price.setText("$" + r.getR_price() + "");
            v.add(r.getR_rommber());
            R_body.add(v);
        }
        leftModel.setDataVector(R_body, header);
    }

    /**
     * 设置左边的table的信息
     *
     * @param roomtype
     */
    private void addLeftModel(String roomtype) {

        Vector v = new Vector();
        v.add(roomtype);
        this.rightBody.add(v);
        rightModel.setDataVector(rightBody, rightheader);
        try {
            if (rightBody.firstElement() != null) {
                roomnumber.setText(rightBody.firstElement().get(0).toString());
            }
        } catch (Exception e) {
        }
    }

    /**
     * @param args
     */
    public static void main(String[] args) {
        new BillingInformationView();
    }
}
